package com.parking.mgr.common.exception.enums;


import com.parking.mgr.common.annotion.ExpEnumType;
import com.parking.mgr.common.constant.ExpEnumConstant;
import com.parking.mgr.common.exception.BaseException;
import com.parking.mgr.common.exception.ServiceException;
import com.parking.mgr.common.exception.asserts.Assertion;
import com.parking.mgr.common.factory.ExpEnumCodeFactory;

/**
 * @ClassName ServerExceptionEnum
 * @Description 服务器内部相关异常枚举
 * @Auther: zhijianpu
 * @Date: 2023/5/18
 */
@ExpEnumType(module = ExpEnumConstant.CORE_MODULE_EXP_CODE, kind = ExpEnumConstant.SERVER_EXCEPTION_ENUM)
public enum ServerExceptionEnum implements IBaseExceptionEnum, Assertion {

    /**
     * 当前请求参数为空或数据缺失
     */
    REQUEST_EMPTY(1, "当前请求参数为空或数据缺失"),
    I18N_MESSAGE_REQUEST_EMPTY(1, "i18n_message_request_empty"),

    /**
     * 服务器出现未知异常
     */
    SERVER_ERROR(2, "服务异常，无参数、无对应方法或数据错误"),
    I18N_MESSAGE_SERVER_ERROR(2, "i18n_message_server_error"),

    /**
     * 常量获取存在空值
     */
    CONSTANT_EMPTY(3, "常量获取存在空值，请检查sys_config中是否配置"),

    /**
     * 正在处理中，请稍后再试
     */
    NO_FINISH(4, "正在处理中，请稍后再试");

    private final Integer code;

    private final String message;

    ServerExceptionEnum(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    @Override
    public Integer getCode() {
        return ExpEnumCodeFactory.getExpEnumCode(this.getClass(), code);
    }

    @Override
    public String getMessage() {
        return message;
    }

    @Override
    public BaseException onAssertFailed(String errorMessage) {
        if (errorMessage == null || errorMessage.isEmpty()) {
            return new ServiceException(this);
        } else {
            return new ServiceException(this.getCode(), errorMessage);
        }
    }
}
